package IceAssembly;

/**
 * Class of conversion functions that are neccesary to take care of tricky conversions
 * of data types, or other processes helpful to prepare the data to be a better
 * format for other operations
 */
// <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
// #[regen=yes,id=DCE.30AAA218-F341-312C-13D2-067C8C406283]
// </editor-fold> 
public class Convert {

    /**
     * Converts binary values to the equivalent in hexadecimal. The only binary 
     * encoding handled is regular. Additionally the hex value returned will be 
     * in lowercase. For example, binary 1010 is 10 in int, and a in hex.
     * <p>
     * Changelog:<br>
     * April 13 - Initial code - Peter Dietz 
     * April 16 - Clarified Documentation - Peter Dietz
     * <p>
     * Errors Checked For:<br>
     * None<p>
     * <p>
<b>Original Author:</b> Peter Dietz
<p>
     * @param  binarystring  string encoded with binary value
     * @return string representing hexadecimal equivalent (lower case)
     */
    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.BEB56647-E5F9-4527-E0CD-8DF5C568EDF1]
    // </editor-fold> 
    public String binaryToHex (String binarystring){ 
        int binaryint = Integer.parseInt(binarystring, 2);       
        return String.valueOf(Integer.toHexString(binaryint));       
    }

    /**
     *Takes a raw string that may contain comments, and removes them. It looks 
     * for the semicolon (:) and removes that and everything following it in the
     * line. If the entire string was comment, then it returns the empty string. 
     * <p>
     * Changelog:<br> 
     * Initial code - April 13 - Peter Dietz 
     * <p>
<b>Original Author:</b> Peter Dietz
<p>
     * @param  randomstring Line of assembly source possibly laden with comments
     * @return string free of comments 
     */
    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.89C6C877-BE34-0B2F-A4E3-C18268D5CD9A]
    // </editor-fold> 
    public String getNonCommentString (String randomstring) {
        String noncomment;
        
        if (randomstring.contains(":"))
        {
            int firstsemicolon = randomstring.indexOf(":");
            noncomment = randomstring.substring(0, firstsemicolon);
        } else {
            noncomment = randomstring;
        }
        return noncomment;
    }

    /**
     * Converts an integer to hexadecimal, making sure that the hex value has 
     * a length of four. It will be prepended with 0's to make the length of four.
     * <p>
     * Changelog:<br> 
     * April 18 - found need for operation and coded it - Peter Dietz 
     * <p>
<b>Original Author:</b> Peter Dietz
<p>
     * @param  int_value Value that needs to be converted to hex with four digits
     * @return string representation of int_value in four hex numbers
     */
    public String intToFourHex (int int_value){
        String hex_value = String.valueOf(Integer.toHexString(int_value));
        while(hex_value.length() < 4){
            hex_value = "0".concat(hex_value);
        }
        return hex_value;
    }    
    
    /** 
     *Turns multiple spaces into a single space (leaving the spaces between 
     *functions and parameters). Additionally, remaining whitespace after the 
     * last non-whitespace-character will be truncated.
     *<p>
     *Log File:<br> 
     *April 13 - Initial code - Peter Dietz<br>
     *April 13 - Revised Documentation - Peter Dietz<br>
     *April 16 - Had to remove trailing whitespace - Peter Dietz<br>
     *<p> 
     * Error Conditions Tested:<br>
     * Nothing yet<br>
     * <p>
     * Error Messages Generated: <br>
     * None yet<br>
     * <p>
     * Date of module being installed: April 13
     * <p>
<b>Original Author:</b> Peter Dietz
<p>
     *@param  linestring Line of assembly source that may have many whitespaces
     *@return string with less spaces
     */
    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.9296AB66-12A8-8A61-D424-9ED2587EA067]
    // </editor-fold> 
    public String optimizeTheSpaces (String linestring) {
        int i = 0;
        while (i < linestring.length())
        {
            linestring = linestring.replaceAll("  "," ");
            // In case there is a \t special character 
             linestring = linestring.replaceAll(" \t"," ");
            linestring = linestring.replaceAll("\t "," ");
            linestring = linestring.replaceAll("\t\t"," ");
            linestring = linestring.replaceAll("\t"," ");
            i++;
        }
        if (linestring.endsWith(" ")){
            linestring = linestring.substring(0, linestring.length()-1);
        }
        return linestring;
    }

    /** 
     *Convert an n type decimal parameter to the binary string equivalent that
     * is length bits long.
     *<p>
     *Log File:<br> 
     *April 25 - Initial Code - Peter Dietz.<br>
     *April 25 - Allow any length of bit strings.<br>
     *May 9 - Changed the type to public - Needed by Other Package - Yogesh Jindal.<br>
     * May 26 - Made a fix for incase the binary is already longer than length. Such is the case for negative. Just take rightmost - Peter Dietz<br>
     *<p> 
     *<b>Error Conditions Tested:</b><br>
     * Nothing yet<br>
     * <p>
     * Date of module being installed: April 25
     * <p>
<b>Original Author:</b> Peter Dietz
<p>
     *@param  parameter positive decimal integer whose value is n, nn, nnn, or 
     * nmax
     *@param length length needed for output binary string
     *@return length bit binary string of parameter
     */
    public String getBitCode(String parameter, int length){
        String temp = Integer.toBinaryString(Integer.valueOf(parameter));
        if(temp.length()>length){
            temp = temp.substring(temp.length()-length);
        }
        while (temp.length() < length ){
            temp = "0".concat(temp);
        }
        return temp;
    }
}